<?php

App::uses('Shell', 'Console');
App::uses('BaseParser', 'Console/Command/Task');

class LeituraM02Task extends BaseParser {
    public $uses = array('LeiturasM02');

    public function execute() {
        $this->out('LeituraM02');

        $this->parse();
    }

    private function parse() {
        $this->content = explode("\n", $this->content);
    
        unset($this->content[0]);
        unset($this->content[1]);

        $db = $this->LeiturasM02->getDataSource();
        $db->begin();

        foreach ($this->content as $line) {
            $line = trim($line);
            $line = str_replace(',','.',$line);
            $itens = preg_split("/\t+/", $line);

            if (count($itens) < 30)
                continue;

            $horaSplit = preg_split("/ +/", $itens[1]);
                        

            $date = $this->convertToDateSql2($itens[0], $horaSplit[0], $horaSplit[1]);

            $sequencia = $this->getNumSequencia($date);

            $leitura = array(
                    'LeiturasM02' => array(
                        'sequencia'     => $sequencia,
                        'hora'          => $date,
                        'estacao_id'    => $this->estacaoId,
                        'temp_out'      => $itens[2],
                        'hi_temp'       => $itens[3],
                        'low_temp'      => $itens[4],
                        'out_hum'       => $itens[5],
                        'dew_pt'        => $itens[6],
                        'wind_speed'    => $itens[7],
                        'wind_dir'      => $itens[8],
                        'wind_run'      => $itens[9],
                        'hi_speed'      => $itens[10],
                        'hi_dir'        => $itens[11],
                        'wind_chill'    => $itens[12],
                        'heat_index'    => $itens[13],
                        'thw_index'     => $itens[14],
                        'bar'           => $itens[15],
                        'rain'          => $itens[16],
                        'rain_rate'     => $itens[17],
                        'heat_d_d'      => $itens[18],
                        'cool_d_d'      => $itens[19],
                        'in_temp'       => $itens[20],
                        'in_hum'        => $itens[21],
                        'in_dew'        => $itens[22],
                        'in_heat'       => $itens[23],
                        'in_emc'        => $itens[24],
                        'in_air_density'=> $itens[25],
                        'wind_samp'     => $itens[26],
                        'wind_tx'       => $itens[27],
                        'iss_recept'    => $itens[28],
                        'arc_int'       => $itens[29]
                    )
                );

            if (!$this->LeiturasM02->save($leitura)) {
                $this->out('Erro ao adicionar Leitura, erros: '.print_r($this->LeiturasM02->validationErrors, true));
            }
        }

        if ($db->commit() === false) {
            $this->out('Erro ao adicionar Leitura, erros: '.print_r($this->LeiturasM02->validationErrors, true));
        }
    }

    private function convertToDateSql2($date, $hours, $ammp) {
        $date = $date.' '.$hours.':00';

        $myDateTime = DateTime::createFromFormat('d/m/y H:i:s', $date);

        if ($ammp == 'p')
            $myDateTime->add(new DateInterval('PT12H'));

        if ($myDateTime == null)
            return '';
        
        return $myDateTime->format('Y-m-d H:i:s');
    }
}